package com.yami.trading.huobi.data.job;


import cn.hutool.core.util.StrUtil;
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.common.util.Arith;
import com.yami.trading.common.util.MarketOpenChecker;
import com.yami.trading.common.util.ThreadUtils;
import com.yami.trading.common.util.UTCDateUtils;
import com.yami.trading.huobi.data.AdjustmentValueCache;
import com.yami.trading.huobi.data.DataCache;
import com.yami.trading.huobi.data.internal.DataDBService;
import com.yami.trading.huobi.data.model.AdjustmentValue;
import com.yami.trading.huobi.hobi.HobiDataService;
import com.yami.trading.service.item.ItemService;
import com.yami.trading.service.syspara.SysparaService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Collectors;

@Slf4j
@Component
public class ForexGetDataJob extends AbstractGetDataJob {


    public void run() {

        if (first) {
            // data数据保存间隔时长(毫秒)
            this.interval = this.sysparaService.find("data_interval").getInteger().intValue();
            first = false;
        }
        while (true) {
            try {
                // 老外汇tradeView接口
                String symbols = itemService.findByType(Item.forex)
                        .stream().filter(i -> !i.getCategory().equalsIgnoreCase(Item.forex))
                        .map(Item::getSymbol).collect(Collectors.joining(","));

                // 新外汇
                String xinLangsymbols = itemService.findByType(Item.forex)
                        .stream().filter(i -> i.getCategory().equalsIgnoreCase(Item.forex))
                        .map(Item::getSymbol).collect(Collectors.joining(","));

                SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                f.setTimeZone(TimeZone.getTimeZone(UTCDateUtils.GMT_TIME_ZONE));
//		        log.info("当前UTS标准时间 {}", f.format(new Date()));
//				log.info("休市时间 {}" ,f.format(UTCDateUtils.getClosedTime()));
//				log.info("开市时间 {}", f.format(UTCDateUtils.getOpenTime()));
                Date now = f.parse(f.format(new Date()));
                if (MarketOpenChecker.isMarketOpenByItemCloseType(Item.forex)) {
                    this.realtimeHandle(symbols);
                    this.realtimeHandleXinLang(xinLangsymbols);
                } else {
                    logger.debug("当前为休市时间，当前UTS标准时间：{}, 开市时间：{}, 休市时间：{}",
                            f.format(new Date()), f.format(UTCDateUtils.getOpenTime()), f.format(UTCDateUtils.getClosedTime()));
                }
            } catch (Exception e) {
                logger.error("run fail", e);
            } finally {
                ThreadUtils.sleep(this.interval);
            }
        }

    }

    @Override
    public String getName() {
        return "外汇实时数据采集";
    }


    public void realtimeHandle(String symbols) {
        if (StrUtil.isEmpty(symbols)) {
            log.error("当前没有行情数据可以采集");
            return;
        }
        List<Realtime> realtimeList = this.hobiDataService.realtime(symbols);
        super.handleRealTimeList(realtimeList);
    }
    public void realtimeHandleXinLang(String symbols) {
        if (StrUtil.isEmpty(symbols)) {
            log.error("当前没有行情数据可以采集");
            return;
        }
        List<Realtime> realtimeList = this.hobiDataService.realtimeXinLang(symbols);
        super.handleRealTimeList(realtimeList);
    }

}
